//,editrules:{required:true,number:true,minValue:0,maxValue:1} 数字
$(function() {
	var initData = $("#initData").val();
	var ss = initData.split(",");
	initLoad(ss);
});

function initLoad(ss) {
	var datas = {
		"sortorder": "ASC"
	};
	var cols = ['ID', '标题', '内容', '封面','类型', '标签', '状态', '时间', '操作'];
	var models = [{
		name: 'id',
		index: 'id',
		width: 200,
		fixed: true,
		key: true,
		search: false,
		editable: false,
		editoptions: {
			readonly: true
		}
	},
	{
		name: 'title',
		index: 'title',
		searchoptions: {
			'sopt': ['cn']
		}
	},
	{
		name: 'intro',
		index: 'intro',
		search: false,
	},
	{
		name: 'cover',
		index: 'cover',
		search: false,
		align: 'center',
		width: 91,
		fixed: true,
		formatter: function(cv, op, rb) {
			if (cv != null) {
				return '<img src="' + cv + '" width="64px"/>';
			} else {
				return '';
			}
		}
	},
	{
		name: 'type',
		index: 'type',
		search: false
	},
	{
		name: 'flag',
		index: 'flag',
		search: false
	},
	{
		name: 'status',
		index: 'status',
		hidden: false,
		search: false,
		formatter: function(cv, op, rb){
		    if(cv == "0"){
		        return "草稿";
		    }
            if(cv == "1"){
		        return "自己";
		    }
		    if(cv == "2"){
		        return "发布";
		    }
		}
	},

	{
		name: 'createTime',
		index: 'createTime',
		search: false,
		width: 200,
		formatter: function(cv, op, rb) {
			return num2Date(cv);
		}
	},
	{
		name: 'opt',
		index: 'id',
		search: false,
		formatter: function(cv, op, rb) {
			var html = '';
			html += '<a target="_blank" href="/blog/article/read/' + rb.id + '">&nbsp;查看</a>&nbsp;';
			return html;
		}
	}];

	loadData("/master/article", "tabdiv", datas, cols, models);
}

//重载使用
function loadTab(tabDiv, datas) {
	jQuery("#" + tabDiv).jqGrid('setGridParam', {
		datatype: 'json',
		postData: datas
	}).trigger("reloadGrid");
}

//首载使用
function loadData(uriPrefix, tabDiv, datas, cols, models) {
	if (datas.sortorder === undefined) {
		datas.sortorder = 'DESC';
	}
	var hv = $(document).height() - 121;
	$.jgrid.defaults.styleUI = 'Bootstrap';
	$("#" + tabDiv).jqGrid({
		sortorder: datas.sortorder,
		postData: datas,
		url: uriPrefix + '/list',
		datatype: 'json',
		mtype: 'POST',
		rownumbers: true,
		autowidth: true,
		altRows: true,
		shrinkToFit: true,
		rowNum: 20,
		rowList: [20, 50, 100],
		colNames: cols,
		colModel: models,
		pager: '#' + tabDiv + '_page',
		viewrecords: true,
		hidegrid: false,
		multiselect: true,
		height: hv,
		editurl: uriPrefix + '/crud',
		loadComplete: function(a, b, c) {
			$('#' + tabDiv).jqGrid('setLabel', 'rn', 'No.', {
				'text-align': 'center',
				'vertical-align': 'middle',
				"width": "65"
			});
			$("table[id='tabdiv'] tr").each(function(i, d) {
				$(d).children("td:first").width(52)
			});
		}

	});

	// 底部CURD操作按钮
	$("#" + tabDiv).jqGrid("navGrid", "#" + tabDiv + "_page", {
		edit: false,
		add: false,
		del: true,
		search: true
	},
	{
		closeAfterEdit: true,
		reloadAfterSubmit: true,
		afterComplete:function (a,data,c) {
		    var res = a.responseText;
		    if("ok" != res){
		        alert(res);
		    }
		}
	},
	{
		closeAfterAdd: true,
		reloadAfterSubmit: true,
		beforeShowForm:function(formId){
            console.log(formId)
        },
		afterComplete:function (a,data,c) {
		    var res = a.responseText;
		    if("ok" != res){
		        alert(res);
		    }
		    //data 是提交的数据对象表单
		}
	},
	{
		closeAfterDel: true,
		reloadAfterSubmit: true,
		afterComplete:function (a,data,c) {
		    var res = a.responseText;
		    if("ok" != res){
		        alert(res);
		    }
		}
	},
	{});

    //显示序号
	$("#" + tabDiv).jqGrid('setLabel', 'rn', 'No', {'text-align': 'left' },'');

    //窗体改变了，改变表格宽度
    $(window).bind('resize',function() {
        window.location.reload();
    });
}
